package com.itheima.reggie.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R page(Integer page, Integer pageSize) {
//        Page<Orders> pageInfo = new Page<>(page,pageSize);
//        orderService.page(pageInfo);
//        return R.success(pageInfo);
//
        Map data=orderService.pageInfo(page,pageSize);
        return R.success(data);
    }

    @GetMapping("/page")
    public R page1(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        IPage pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(number), Orders::getNumber, number);
        qw.gt(StrUtil.isNotEmpty(beginTime), Orders::getOrderTime, beginTime);
        qw.lt(StrUtil.isNotEmpty(endTime), Orders::getOrderTime, endTime);


        orderService.page(pageInfo, qw);
        List records = pageInfo.getRecords();
        long total = pageInfo.getTotal();
        Map data = new HashMap();
        data.put("records", records);
        data.put("total", total);
        return R.success(pageInfo);
    }

    @PutMapping
    public R updateStatus(@RequestBody Orders orders) {
        LambdaUpdateWrapper<Orders> qw = new LambdaUpdateWrapper<>();
        Integer status = orders.getStatus();

        qw.eq(Orders::getId, orders.getId());
        qw.set(Orders::getStatus,status);
        orderService.update(qw);
        return R.success(null);
    }

    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
       orderService.again(orders);

        return R.success(null);
    }
}